package cn.dansj.controller;

import cn.dansj.common.request.RestTemplateUtils;
import cn.dansj.common.request.annotation.http.GetMethod;
import cn.dansj.common.request.annotation.http.PostMethod;
import cn.dansj.common.request.annotation.http.RequestMappingMethod;
import cn.dansj.common.request.annotation.http.RequestParam;
import cn.dansj.common.utils.bean.Return;
import cn.dansj.common.utils.json.DictMap;
import cn.dansj.common.utils.transfer.Verification;
import cn.dansj.enums.GlobalEnv;

import java.util.List;

@RequestMappingMethod("/nat")
public class NatController {
    private static final String NatUsername = "dansj";
    private static final String NatPassword = "Sunjie!@#$%^&*";
    private static final RestTemplateUtils restTemplateUtils = RestTemplateUtils.builder().basicAuth(NatUsername, NatPassword).build();

    private String getUrl(String function) {
        return GlobalEnv.NatHost + function;
    }

    private static Return<Object> transferToReturn(DictMap returnValue) {
        Return<Object> returnObj = new Return<>();
        int resultCode = returnValue.getIntValue("resultCode");
        return returnObj.setStatus(resultCode == 200).setResponseCode(resultCode == 200 ? Return.SUCCESS_CODE : Return.FAIL_CODE + 1).setData(returnValue.get("data")).setMessage(returnValue.getString("msg"));
    }

    @GetMethod(value = "/clientList")
    public Return<?> getList() {
        DictMap clientList = restTemplateUtils.get(getUrl("clientList"), DictMap.class);
        List<DictMap> data = clientList.getList("data", DictMap.class).stream().peek(item -> item.remove("pwd")).toList();
        clientList.put("data", data);
        return transferToReturn(clientList);
    }

    @PostMethod("/modifyClient")
    public Return<?> modifyClient(@RequestParam long key, @RequestParam(required = false) String newKey, @RequestParam(decrypt = true) String pwd, @RequestParam String name, @RequestParam boolean status) {
        DictMap requestParams = DictMap.of("key", key, "newKey", Verification.checkNotNull(newKey) ? Long.parseLong(newKey) : null, "pwd", pwd, "name", name, "status", status);
        return transferToReturn(restTemplateUtils.post(getUrl(Verification.checkNotNull(newKey) ? "modifyClient" : "addClient"), requestParams, DictMap.class));
    }

    @PostMethod("/removeClient")
    public Return<?> removeClient(@RequestParam long key) {
        return transferToReturn(restTemplateUtils.post(getUrl("removeClient"), DictMap.of("key", key), DictMap.class));
    }

    @PostMethod("/shotClient")
    public Return<?> shotClient(@RequestParam long key) {
        return transferToReturn(restTemplateUtils.post(getUrl("shotClient"), DictMap.of("key", key), DictMap.class));
    }

    @PostMethod("/modifyClientPortMapping")
    public Return<?> modifyClientPortMapping(@RequestParam DictMap requestParam) {
        return transferToReturn(restTemplateUtils.post(getUrl(Verification.checkNotNull(requestParam.getString("oldServerPort")) ? "modifyClientPortMapping" : "addClientPortMapping"), requestParam, DictMap.class));
    }

    @PostMethod("/removeClientPortMapping")
    public Return<?> removeClientPortMapping(@RequestParam long key, @RequestParam int serverPort) {
        return transferToReturn(restTemplateUtils.post(getUrl("removeClientPortMapping"), DictMap.of("key", key, "serverPort", serverPort), DictMap.class));
    }

    @PostMethod("/listenPort")
    public Return<?> listenPort(@RequestParam long key, @RequestParam int port) {
        return transferToReturn(restTemplateUtils.post(getUrl("listenPort"), DictMap.of("key", key, "port", port), DictMap.class));
    }

    @PostMethod("/closePort")
    public Return<?> closePort(@RequestParam long key, @RequestParam int port) {
        return transferToReturn(restTemplateUtils.post(getUrl("closePort"), DictMap.of("key", key, "port", port), DictMap.class));
    }
}
